package com.atguigu.tingshu.album.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.atguigu.tingshu.common.constant.RabbitConstant;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.AlbumInfoSyncData;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

@CanalEventListener
@Slf4j
public class CanalClientListener {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @ListenPoint(destination = "example",schema = "tingshu_album",table = {"album_info"},eventType = {CanalEntry.EventType.UPDATE})
    public void upDateAlbum(CanalEntry.RowData rowData){

        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        afterColumnsList.forEach(column -> {
            String columnName = column.getName();
            if ("id".equalsIgnoreCase(columnName)){
                redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+column.getValue());
            }
        });
    }

    @ListenPoint(destination = "example",schema = "tingshu_album",table = {"album_stat"},eventType = {CanalEntry.EventType.UPDATE})
    public void updateAlbumStat(CanalEntry.RowData rowData){
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        AlbumInfoSyncData albumInfoSyncData = new AlbumInfoSyncData();
        afterColumnsList.forEach(column -> {
           if ("album_id".equalsIgnoreCase(column.getName())){
               //删除redis中存在的专辑统计信息
               redisTemplate.delete(RedisConstant.ALBUM_STAT_INFO+column.getValue());
               log.info("从redis中删除专辑统计数据："+column.getValue());
               albumInfoSyncData.setAlbumId(column.getValue());
           }
           if ("stat_type".equalsIgnoreCase(column.getName())){
               albumInfoSyncData.setStatType(column.getValue());
           }
           if ("stat_num".equalsIgnoreCase(column.getName())){
               albumInfoSyncData.setStatNum(column.getValue());
           }
        });
           //同步修改es中的专辑统计信息,用mq实现
            rabbitTemplate.convertAndSend(RabbitConstant.SEARCH_EXCHANGE,RabbitConstant.UPDATE_ALBUM_STAT_ROUTING, JSON.toJSONString(albumInfoSyncData));
    }

    @ListenPoint(destination = "example",schema = "tingshu_album",table = {"base_category1","base_category2","base_category3"},eventType = {CanalEntry.EventType.UPDATE})
    public void updateCategory(CanalEntry.RowData rowData){
        //删除redis中全部的专辑分类信息
        redisTemplate.delete(RedisConstant.ALBUM_CATEGORY_INFO+"*");
    }

    @ListenPoint(destination = "example",schema = "tingshu_user",table = {"user_info"},eventType = {CanalEntry.EventType.UPDATE})
    public void updateUserInfo(CanalEntry.RowData rowData){
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        String nickName = "";
        for (CanalEntry.Column column : afterColumnsList) {
            if ("id".equalsIgnoreCase(column.getName())){
                redisTemplate.delete(RedisConstant.ALBUM_USER_INFO+column.getValue());
                log.info("从redis中删除用户信息："+column.getValue());
            }
            if ("nickname".equalsIgnoreCase(column.getName())){
                nickName = column.getValue();
            }
        }
        //发送mq消息，更新用户信息
        rabbitTemplate.convertAndSend(RabbitConstant.SEARCH_EXCHANGE,RabbitConstant.UPDATE_USER_NICKNAME_ROUTING,nickName);
    }
}
